package com.hzz.web.boss.analysis;

import com.hzz.api.analysis.IAnalysisRiverSupervisorService;
import com.hzz.dto.analysis.AnalysisRiverSupervisorDto;
import com.hzz.framework.PageUtil;
import com.hzz.framework.Response;
import com.hzz.framework.annotation.auth.RequiresPermissions;
import com.hzz.framework.util.DateUtil;
import com.hzz.framework.util.FileUtil;
import com.hzz.framework.util.StrUtil;
import com.hzz.framework.util.WebUtil;
import com.hzz.framework.web.controller.BaseController;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.FileUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.List;
import java.util.Map;

@Api("河长统计分析")
@RestController
@Slf4j
public class AnalysisRiverSupervisorController extends BaseController {
    private static final long serialVersionUID = -528422099490438672L;
    private static final String acPrefix = "/boss/analysis/river/supervisor/";
    @Value("${export.analysis_river_supervisor}")
    String templateXls;
    @Autowired
    IAnalysisRiverSupervisorService analysisRiverSupervisorService;

    /**
     * <p>河长统计。
     */
    @RequiresPermissions("analysis:river:supervisor")
    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = acPrefix + "area/page/{pageNum}")
    // @ApiOperation(value = "各级河长数量-分页")
    public Response analysisRiverSupervisorAreaPage(@ModelAttribute AnalysisRiverSupervisorDto dto, @PathVariable("pageNum") Integer pageNum) {
        log.info("analysisRiverSupervisorAreaPage.........");
        Response result = new Response();
        try {
            dto.setPageSize(5);
            dto.setPageNum(pageNum);
            result.data = PageUtil.copy(analysisRiverSupervisorService.analysisRiverSupervisorAreaPage(dto));
        } catch (Exception e) {
            result = Response.error(e.getMessage());
        }
        return result;
    }

    /**
     * <p>河长统计。
     */
    @RequiresPermissions("analysis:river:supervisor")
    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = acPrefix + "area/level")
    // @ApiOperation(value = "各级河长数量-all")
    public Response analysisRiverSupervisorAreaLevel(@ModelAttribute AnalysisRiverSupervisorDto dto) {
        log.info("analysisRiverSupervisorAreaLevel.........");
        Response result = new Response();
        try {
            result.data = analysisRiverSupervisorService.analysisRiverSupervisorAreaLevel(dto);
        } catch (Exception e) {
            result = Response.error(e.getMessage());
        }
        return result;
    }

    /**
     * <p>河长统计。
     */
    @RequiresPermissions("analysis:river:supervisor")
    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = acPrefix + "area/name")
    // @ApiOperation(value = "次级行政区划河长数量")
    public Response analysisRiverSupervisorAreaName(@ModelAttribute AnalysisRiverSupervisorDto dto) {
        log.info("analysisRiverSupervisorAreaName.........");
        Response result = new Response();
        try {
            result.data = analysisRiverSupervisorService.analysisRiverSupervisorChildAreaName(dto);
        } catch (Exception e) {
            result = Response.error(e.getMessage());
        }
        return result;
    }

    /**
     * <p>河长统计 导出。
     */
    @RequiresPermissions("analysis:river:supervisor:export:xls")
    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = acPrefix + "export/xls")
    // @ApiOperation(value = "行政区划河长数量导出")
    public void analysisRiverSupervisorExport(@ModelAttribute AnalysisRiverSupervisorDto dto) throws Exception {
        log.info("analysisRiverSupervisorExport.........");
        try {
            setResponseHeader(request, response, "河长统计_" + DateUtil.getCurDateStr("yyyyMMddHHmmss") + ".xls");
            List<Map> dataIsList = analysisRiverSupervisorService.analysisRiverSupervisorAreaName(dto);
            //创建模板的副本
            InputStream inputStream = FileUtil.getFileAsInputStream(templateXls);
            File temp = new File(System.getProperty("java.io.tmpdir"), "analysis_river_supervisor_template_" + DateUtil.getCurDateStr("yyyyMMddHHmmss") + ".xls");
            FileUtils.copyInputStreamToFile(inputStream, temp);
            //表格s数据填充
            InputStream input = new FileInputStream(temp);
            HSSFWorkbook workBook = new HSSFWorkbook(input);
            Sheet sheet = workBook.getSheetAt(0);
            try {
                for (int i = 0; i < dataIsList.size(); i++) {
                    Map data = dataIsList.get(i);
                    //从第2行开始写
                    Row row = sheet.createRow(i + 1);

                    row.createCell(0).setCellValue(StrUtil.asString(data.get("areaName")));//行政区划
                    row.createCell(1).setCellValue(StrUtil.asString(data.get("level3Count")));
                    row.createCell(2).setCellValue(StrUtil.asString(data.get("level4Count")));
                    row.createCell(3).setCellValue(StrUtil.asString(data.get("level5Count")));
                }
            } catch (Exception e) {
                log.error("excel数据导出填充异常!", e);
            }
            workBook.write(response.getOutputStream());
            workBook.close();
            input.close();
            FileUtil.closeStream(inputStream);
            temp.delete();
        } catch (Exception e) {
            log.error("excel数据导出异常", e);
            WebUtil.sendJson(response, Response.error(500, "excel数据导出异常!" + e.getMessage()));
        }
    }
}